﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SmsUI.DataBase;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace SmsUI
{
    public partial class SendSMSForm : DevExpress.XtraEditors.XtraUserControl
    {
        DataBase.smsdbDataSet.tb_ContentDataTable tb = null;
        DataBase.smsdbDataSet.tb_ConfigDataTable tbConfig = null;
        public SendSMSForm()
        {
            InitializeComponent();
            btnDelete.Enabled = false;
            btnModify.Enabled = false;
            tb = DbRules.Instance.GetContentTable();
            tbConfig = DbRules.Instance.GetConfigInfo();
            lbContentList.DataSource = tb;
            string randomAdd = ConfigManager.GetValue("randomAdd");

            if (randomAdd == "0")
            {
                this.checkEdit1.Checked = false;
            }
            if (randomAdd == "1")
            {
                this.checkEdit1.Checked = true;
            }
          

            this.textEdit1.Text = ConfigManager.GetValue("randomAddString");



        }

        private void tbContent_TextChanged(object sender, EventArgs e)
        {
            this.lbWordsCount.Text = string.Format("短信内容可以为中英文，允许总长度70个字，当前长度{0}个字", tbContent.Text.Length);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            tb.Addtb_ContentRow(tbContent.Text);
            DbRules.Instance.UpdateContent();
            tb.AcceptChanges();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (lbContentList.SelectedIndex > -1)
            {
                tb[lbContentList.SelectedIndex].Content = tbContent.Text;
                DbRules.Instance.UpdateContent();
                tb.AcceptChanges();
            }
            else
            {
                XtraMessageBox.Show("没有选中内容，不能修改!");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            tb[lbContentList.SelectedIndex].Delete();

            DbRules.Instance.UpdateContent();
            tb.AcceptChanges();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {

            for (int i = tb.Rows.Count - 1; i > -1; i--)
            {
                tb.Rows[i].Delete();
            }
            DbRules.Instance.UpdateContent();
            tb.AcceptChanges();
        }

        private void lbContentList_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                if (lbContentList.SelectedIndex > -1)
                {
                    if (tb[lbContentList.SelectedIndex].RowState != DataRowState.Deleted)
                    {
                        tbContent.Text = tb[lbContentList.SelectedIndex].Content;
                        btnDelete.Enabled = true;
                        btnModify.Enabled = true;
                    }
                }
                else
                {
                    btnDelete.Enabled = false;
                    btnModify.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                tbContent.Text = "";
            }
        }

        private void checkEdit1_CheckedChanged(object sender, EventArgs e)
        {


            if (this.checkEdit1.Checked == false)
            {
                ConfigManager.SetValue("randomAdd", "0");
            }
            if (this.checkEdit1.Checked == true)
            {
                ConfigManager.SetValue("randomAdd", "1");
            }


            //ConfigManager.RandomAdd = this.checkEdit1.Checked;
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {
            ConfigManager.SetValue("randomAddString", this.textEdit1.Text);
        }
    }
}
